package com.yidian.controller;

import com.yidian.common.entity.Result;
import com.yidian.entity.Channel;
import com.yidian.entity.User;
import com.yidian.service.impl.UserServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/user")
@Api(value = "/user", tags = "用户接口")
public class UserController {

    @Autowired
    private UserServiceImpl userService;

    /**
     * 获取个人信息
     * @param userId
     * @return
     */
    @GetMapping("/userMsg/{userId}")
    @ApiOperation(value = "获取个人信息", notes = "获取个人信息", httpMethod = "GET", response = Result.class)
    public Result getUserInfo(@PathVariable Long userId) {

        User user = userService.selectByUserId(userId);
        return Result.ok(user);
    }

    /**
     * 获取用户的频道信息
     * @param id
     * @return
     */
    @GetMapping("/channels/{userId}")
    @ApiOperation(value = "获取用户的频道信息", notes = "获取用户的频道信息", httpMethod = "GET", response = Result.class)
    public Result getUserChannels(@PathVariable("userId") long id) {

        List<Channel> channels = userService.selectChannelByUserId(id);
        return Result.ok(channels);
    }

    /**
     * 取消关注用户
     * @param upId
     * @param id
     * @return
     */
    @RequestMapping(value = "/deleteFollow/{upId}/{id}", method = RequestMethod.DELETE)
    @ApiOperation(value = "取消关注用户", notes = "取消关注用户", httpMethod = "DELETE", response = Result.class)
    public Result deleteFollow(@PathVariable("upId") long upId, @PathVariable("id") long id) {

        userService.deleteFollow(upId, id);
        return Result.ok("取消关注成功", null);
    }

    /**
     * 关注用户
     * @param upId
     * @param id
     * @return
     */
    @PostMapping("/addFollow/{upId}/{id}")
    @ApiOperation(value = "关注用户", notes = "关注用户", httpMethod = "POST", response = Result.class)
    public Result addFollow(@PathVariable("upId") long upId, @PathVariable("id") long id) {

        userService.addFollow(upId, id);
        return Result.ok("关注成功", null);
    }

    /**
     * 获取指定用户信息
     * @param degUserId
     * @return
     */
    @GetMapping("/DesignatedUser/{degUserId}")
    @ApiOperation(value = "获取指定用户信息", notes = "获取指定用户信息", httpMethod = "GET", response = Result.class)
    public Result getDegUserMsg(@PathVariable("degUserId") long degUserId) {

        User user = userService.selectByPrimaryKey(degUserId);
        return Result.ok(user);
    }

    /**
     * 更新用户资料
     * @param user
     * @return
     */
    @RequestMapping(value = "/updateUserMsg", method = RequestMethod.PATCH)
    @ApiOperation(value = "更新用户资料", notes = "更新用户资料", httpMethod = "PATCH", response = Result.class)
    public Result updateUserMsg(@RequestBody User user) {

        userService.updateUserByUserId(user);
        return Result.ok("更新成功", null);
    }

    /**
     * 更新用户图片
     * @param id
     * @param imgFile
     * @return
     */
    @PostMapping(value = "/updatePhoto/{userId}")
    @ApiOperation(value = "更新用户图片", notes = "更新用户图片", httpMethod = "POST", response = Result.class)
    public Result updatePhoto(@PathVariable("userId") long id, @RequestParam("img") MultipartFile imgFile) {
        if (imgFile==null){
            return Result.fail("上传错误，请选择图片重新上传");
        }
        userService.updatePhotoByUserId(id,imgFile);
        return Result.ok("图片更新成功", null);
    }

}
